/*
 * Author: Andrew Shore
 * CSCE 145
 * Homework 08
 * 	This program creates a database that keeps track of three different kinds of contacts, (family, business, personal),
 * and gives the user several options to interact with the database.
 */
import java.util.Scanner;

public class ContactDatabase {
	Scanner keyboard=new Scanner(System.in);
	//displays choices in a table
	public static void printInfo(){
		
		System.out.println("*********************************************************");
		System.out.println("* Enter 'add' to add a contact to the database.         *");
		System.out.println("* Enter 'remove' to remove a contact in the database.   *");
		System.out.println("* Enter 'list all' to list all contacts in the database.*");
		System.out.println("* Enter 'lookup' to find a contact in the database.     *");
		System.out.println("* Enter 'edit' to edit a contact entry.                 *");
		System.out.println("* Enter 'exit' to quit the program.                     *");
		System.out.println("*********************************************************");
	}
	//allows user to add a contact to the database
	public static void addContact(Contact[] a,Scanner keyboard){
		
		System.out.println("What type of contact is the person (Family, business, personal) ");
		String answer=keyboard.next();
		
		if(answer.equalsIgnoreCase("family")){
			for(int index=0; index<a.length; index++){
				//puts contact in next available slot
				if(a[index]==null){
					keyboard.nextLine();
					System.out.println("Please enter the name of the family member:");
					String name=keyboard.nextLine();
					//calls duplicateName method to make sure no contact is entered twice
					if(!duplicateName(a, keyboard, name)){
					System.out.println("Please enter family member's relationship:");
					String relationship=keyboard.nextLine();
					System.out.println("Please enter the family member's phone number: ");
					String phoneNumber=keyboard.nextLine();
					System.out.println("Please enter the family member's email:");
					String email=keyboard.nextLine();
					FamilyContact theContact=new FamilyContact(name, phoneNumber, email, relationship);
					a[index]=theContact;
					break;
					}
					else{
						System.out.println("Error, duplicate entry.");
						break;
					}
				}
				if( index==a.length-1 && a[a.length-1]!=null){
					System.out.println("Error, Contact Database full.");
					break;
				}
		}
		}
		//the business and personal if statements are the same as above
		else if(answer.equalsIgnoreCase("business")){
			for(int index=0; index<a.length; index++){
				if(a[index]==null){
					keyboard.nextLine();
					System.out.println("Please enter the name of the business contact:");
					String name=keyboard.nextLine();
					if(!duplicateName(a, keyboard, name)){
					System.out.println("Please enter business contact's business name:");
					String relationship=keyboard.nextLine();
					System.out.println("Please enter the business contact's phone number: ");
					String phoneNumber=keyboard.nextLine();
					System.out.println("Please enter the business contact's email:");
					String email=keyboard.nextLine();
					BusinessContact theContact=new BusinessContact(name, phoneNumber, email, relationship);
					a[index]=theContact;
					break;
					}
					else{
						System.out.println("Error, duplicate entry.");
						break;
					}
				}
			if( index==a.length-1 && a[a.length-1]!=null){
				System.out.println("Error, Contact Database full.");
				break;
			}
			}
		}
		else if(answer.equalsIgnoreCase("personal")){
			for(int index=0; index<a.length; index++){
				if(a[index]==null){
					keyboard.nextLine();
					System.out.println("Please enter the name of the personal contact:");
					String name=keyboard.nextLine();
					if(!duplicateName(a, keyboard, name)){
					System.out.println("Please enter how you know this person:");
					String relationship=keyboard.nextLine();
					System.out.println("Please enter the personal contact's phone number: ");
					String phoneNumber=keyboard.nextLine();
					System.out.println("Please enter the personal contact's email:");
					String email=keyboard.nextLine();
					PersonalContact theContact=new PersonalContact(name, phoneNumber, email, relationship);
					a[index]=theContact;
					break;
					}
					else{
						System.out.println("Error, duplicate entry");
						break;
					}
				}
				if( index==a.length-1 && a[a.length-1]!=null){
					System.out.println("Error, Contact Database full.");
					break;
				}
			}
		}
		else{
			System.out.println("Invalid input.");
		}
	}
	//allows user to lookup a contact based on their name
	public static void lookup(Contact[] a, Scanner keyboard, String name){
		
		
		for(int index=0; index<a.length; index++){
			if(a[index]!=null && name.equalsIgnoreCase(a[index].getName())){
				a[index].printInfo();
				break;
			}
			if(index==a.length-1){
				System.out.println("Contact not found");
				
			}		
		}
		
	}
	//looks to see if a name is already in the database and returns false if the name is not found
	public static boolean duplicateName(Contact[] a, Scanner keyboard, String name){
		boolean isName=true;
		
		for(int index=0; index<a.length; index++){
			if(a[index]!=null && name.equalsIgnoreCase(a[index].getName())){
				
				break;
			}
			if(index==a.length-1){
				isName=false;
			}		
		}
		return isName;
	}
	//removes a contact from the database
	public static void removeContact(Contact[]a, Scanner keyboard){
		System.out.println("You have selected remove, please enter the name of the Contact to remove:");
		String name=keyboard.nextLine();
		for(int index=0; index<a.length; index++){
			if(a[index]!=null && name.equalsIgnoreCase(a[index].getName())){
				a[index]=null;
				System.out.println("The contact has been removed from the database.");
			}
			else if(index==25){
				System.out.println("Contact not found.");
			}
		}
	}
	//allows user to edit a contact in the database, very similar to add method
	public static void editContact(Contact[] a, Scanner keyboard){
		System.out.println("You have selected edit; please enter the name of the Contact to edit:");
		String name=keyboard.nextLine();
		for(int index=0; index<a.length; index++){
			if(name.equalsIgnoreCase(a[index].getName())){
				System.out.println("What type of contact is the person (Family, business, personal) ");
				String answer=keyboard.next();
				if(answer.equalsIgnoreCase("family")){
							keyboard.nextLine();
							System.out.println("Please enter the name of the family member:");
							name=keyboard.nextLine();
							System.out.println("Please enter family member's relationship:");
							String relationship=keyboard.nextLine();
							System.out.println("Please enter the family member's phone number: ");
							String phoneNumber=keyboard.nextLine();
							System.out.println("Please enter the family member's email:");
							String email=keyboard.nextLine();
							FamilyContact theContact=new FamilyContact(name, phoneNumber, email, relationship);
							a[index]=theContact;
							break;
			}
				else if(answer.equalsIgnoreCase("business")){
					keyboard.nextLine();
					System.out.println("Please enter the name of the business contact:");
					name=keyboard.nextLine();
					System.out.println("Please enter business contact's business name:");
					String relationship=keyboard.nextLine();
					System.out.println("Please enter the business contact's phone number: ");
					String phoneNumber=keyboard.nextLine();
					System.out.println("Please enter the business contact's email:");
					String email=keyboard.nextLine();
					BusinessContact theContact=new BusinessContact(name, phoneNumber, email, relationship);
					a[index]=theContact;
					break;
				}
				else if(answer.equalsIgnoreCase("personal")){
					keyboard.nextLine();
					System.out.println("Please enter the name of the personal contact:");
					name=keyboard.nextLine();
					System.out.println("Please enter how you know this person:");
					String relationship=keyboard.nextLine();
					System.out.println("Please enter the personal contact's phone number: ");
					String phoneNumber=keyboard.nextLine();
					System.out.println("Please enter the personal contact's email:");
					String email=keyboard.nextLine();
					PersonalContact theContact=new PersonalContact(name, phoneNumber, email, relationship);
					a[index]=theContact;
					break;
					
				}
		}
	}
	}
	//prints all the contacts in the database
	public static void listAll(Contact[] a, Scanner keyboard){
		for(int index=0; index<a.length; index++){
			if(a[index]!=null){
				a[index].printInfo();
				System.out.println("---------------------------");
			}
		}
	}
	
	
	
	public static void main(String args[]){
		Scanner keyboard=new Scanner(System.in);
		Contact[] database=new Contact[25];
		
		System.out.println("Welcome to the ContactDatabase Program.");
		
		String input="";
		
		//loops as long as user does not input exit
		while(!input.equalsIgnoreCase("Exit")){
			ContactDatabase.printInfo();
			input=keyboard.nextLine();
			//calls add method
			if(input.equalsIgnoreCase("Add")){
				ContactDatabase.addContact(database,keyboard);		
			}
			//calls remove method
			if(input.equalsIgnoreCase("remove")){
				ContactDatabase.removeContact(database,keyboard);
			}
			//calls edit method
			if(input.equalsIgnoreCase("edit")){
				ContactDatabase.editContact(database,keyboard);
			}
			//calls lookup method
			if(input.equalsIgnoreCase("lookup")){
				System.out.println("You have selected look-up, please enter the name of the Contact to look-up");
				String name=keyboard.nextLine();
				ContactDatabase.lookup(database, keyboard, name);
			}
			//calls list all method
			if(input.equalsIgnoreCase("list all")){
				ContactDatabase.listAll(database, keyboard);

				
			}
			
			
		}
		
	}
}
